// pages/parkingCoupon/parkingCoupon.js
const app = getApp();
var request = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    windowHeight: 0,
    scrollViewHeight: 0,
    tabs: [{
      id: 0,
      name: '未使用',
      select: true
    },
    {
      id: 1,
      name: '已使用',
      select: false
    },
    {
      id: 2,
      name: '已过期',
      select: false
    },
    ],
    tabIndex: 0,
    couponList: [],
    useCouponList: [],
    pastCouponList: [],
    verifyLoginDialog: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    // 先取出页面高度 windowHeight
    that.setData({
      windowHeight: wx.getSystemInfoSync().windowHeight
    });
    let scrollViewHeight = that.data.windowHeight - that.data.statusBarHeight - 44 - 27 - 22 - 10;
    that.setData({
      scrollViewHeight: scrollViewHeight
    })
  },
  onShow() {
    let token = wx.getStorageSync('lmtc_token');
    if (token) {
      this.getCouponList();
    } else {
      this.setData({
        verifyLoginDialog: true
      })
    }
  },
  verifyLogin() {
    this.getCouponList()
  },
  /**
   * 切换tab
   * @param {*} e 
   */
  selectTab(e) {
    let index = e.target.dataset.index;
    let that = this;
    let tabs = that.data.tabs;
    if (index == this.data.tabIndex) {
      return
    }
    for (let i = 0; i < tabs.length; i++) {
      if (i == index) {
        tabs[i].select = true
      } else {
        tabs[i].select = false;
      }
    }
    that.setData({
      tabIndex: index,
      tabs: tabs,
    })
  },
  bindchange(e) {
    let index = e.detail.current
    let tabs = this.data.tabs;
    for (let i = 0; i < tabs.length; i++) {
      if (i == index) {
        tabs[i].select = true
      } else {
        tabs[i].select = false;
      }
    }
    this.setData({
      tabIndex: index,
      tabs: tabs,
    })
  },
  /**
   * 导航
   * @param {*} event 
   */
  intoMap: function (event) {
    let that = this;
    let index = event.currentTarget.dataset.index;
    let tabIndex = that.data.tabIndex;
    let list = [];
    if (tabIndex == 0) {
      list = that.data.unUsedList;
    } else if (tabIndex == 1) {

      list = that.data.usedList;
    } else {
      list = that.data.pastList;
    }
    let latitude = Number(list[index].latitude)
    let longitude = Number(list[index].longitude)
    wx.openLocation({
      latitude,
      longitude,
      name: list[index].parkName,
      address: list[index].parkAddress,
      scale: 28
    })
  },
  /**
   * 获取优惠券券列表
   */
  getCouponList() {
    let that = this;
    let url = '/customer/ticket/getItemTicket'
    request.http(url, 'POST')
      .then((res) => {
        console.log('优惠券：', res)
        if (res.data.code == 200) {
          that.setData({
            couponList: res.data.data.notExpiredTicketDTOS,
            useCouponList: res.data.data.usedTicketDTOS,
            pastCouponList: res.data.data.expireTicketDTOS
          })
        }
      })
  }
})